package com.example.demo;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;


@RequestMapping("/response")
//@RestController  返回数据
@Controller   //返回视图
public class ResponseController {
    @RequestMapping("/index")
    public String index(){
        return "/index.html";
    }

    @RequestMapping("/indexData")
    @ResponseBody
    public String indexData(){
        return "/index.html";
    }

    @RequestMapping("/indexData2")
    @ResponseBody
    public String indexData2(){
        //浏览器可以自动解析
        return "<h1>牛币666</h1>";
    }

    @RequestMapping("/getMap")
    @ResponseBody
    //Spring根据返回的内容自行决定文本类型，此处返回json
    public HashMap<String,String> getMap(){
        HashMap<String,String> map = new HashMap<>();
        map.put("k1","v1");
        map.put("k2","v2");
        map.put("k3","v3");
        map.put("k4","v4");
        return map;
    }

    //设置状态码
    @ResponseBody
    @RequestMapping("/setStatus")
    public String setStatus(HttpServletResponse response){
        response.setStatus(418);
        return "set status successfully";
    }

    @ResponseBody
    @RequestMapping(value="/setContentType",produces = "application/json",
            method = RequestMethod.GET)
    public String setContentType(){
        return "{\"OK\":\"1\"}";
    }




}
